/**
 * 
 */
package weibo4j.examples;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import weibo4j.Oauth;
import weibo4j.examples.oauth2.Log;
import weibo4j.http.AccessToken;
import weibo4j.http.HttpClient;
import weibo4j.model.PostParameter;
import weibo4j.model.WeiboException;
import weibo4j.org.json.JSONObject;
import weibo4j.util.WeiboConfig;

/**
 * OAuth2
 * @author Administrator
 * 
 */
public class WeiboOAuth2 {
	//根据浏览器访问获取一次性CODE
	//注意redirect_uri必须是重定向以后的首页地址
	//https://api.weibo.com/oauth2/authorize?client_id=1240061702&redirect_uri=http://www.ctosalon.com/index&response_type=code
	//获取授权过的Access Token 
	public static AccessToken access_token(String code){
		Oauth oauth = new Oauth();
		AccessToken access_token = null;
		try {
			access_token=oauth.getAccessTokenByCode(code);
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return access_token;
	}
	//查询用户access_token的授权相关信息
	public static void get_token_info(String access_token){
		try {
			HttpClient client = new HttpClient();
			client.setToken(access_token);
			JSONObject result = client.post("https://api.weibo.com/oauth2/get_token_info",new PostParameter[]{
				new PostParameter("access_token", access_token)
			}).asJSONObject();
			Log.logInfo(result.toString());
		} catch (WeiboException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String access_token = WeiboContants.access_token;
		String uid =WeiboContants.uid;
		String domain =WeiboContants.domain;
		get_token_info(access_token);
  
		
		Date date = new Date(1360241118);
		//System.out.println(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = sdf.format(date);
		System.out.println(s);
	}

}
